package com.app.service.authority;

import com.app.entity.Menu;
import com.app.entity.Pagination;
import com.app.entity.authority.Role;
import com.app.entity.authority.RoleMenuRelation;
import com.app.mapper.authority.RoleMapper;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * Created by lijun on 2018/2/22.
 */
@Service
public class RoleService {

    private final static Logger logger = LoggerFactory.getLogger(RoleService.class);

    @Autowired
    RoleMapper roleMapper;

    //查    role-info列表
    public Pagination queryRoleAll(Pagination page) {
        logger.info("func:queryRoleAll %n page:" + page);

        //排序名
        if (page.getOrderName() == null) {
            page.setOrderName("MODIFYTIME");
        }
        //排序方式
        if (page.getOrderKind() == null) {
            page.setOrderKind("desc");
        }

        ObjectMapper mapper = new ObjectMapper();
        String jsonStr = "";
        String searchName = "";

        if (page.getSearchData() != null) {
            try {
                jsonStr = mapper.writeValueAsString(page.getSearchData());
                Role r = mapper.readValue(jsonStr, Role.class);
                searchName = r.getName();
            } catch (Exception e) {
                logger.error("func:queryRoleAll %n jsonStr:" + jsonStr + "%n" + e.getMessage());
            }
        }

        try{
            List<Role> list = roleMapper.getList(page.getCurrentItem()-1, page.getPageSize(), searchName, page.getOrderName(), page.getOrderKind());
            page.setData(list);

            page.setTotalItems(roleMapper.getListSize(page.getCurrentItem(), page.getPageSize(), searchName));

        }catch(Exception e){
            e.printStackTrace();
        }

        return page;
    }


    //根据ID获取list
    public List<Role> getModifyInfo(Role _role) {
        logger.info("func:getModifyInfo %n _role:" + _role);
        List<Role> dList = roleMapper.getModifyInfo(_role);
        return dList;
    }


    /**
     * 增加角色信息和权限
     * @param _role
     * @return
     */
    @Transactional
    public int insertRoleAll(Role _role) {
        logger.info("func:insertRoleAll %n _role:" + _role.toString());
        Integer result_insertPower=1;
        try {
            _role.setId(java.util.UUID.randomUUID().toString());

            int i=roleMapper.insertRole(_role);
            List<RoleMenuRelation> _relation = _role.getPowerData();

            if(_relation.size() == 0){
                //没有设置权限则不保存
            }else{
                for(RoleMenuRelation value:_relation){
                    //添加 id && roleId
                    value.setRoleId(_role.getId());
                    value.setId(java.util.UUID.randomUUID().toString());
                }
                result_insertPower = roleMapper.insertPower(_relation);
            }
        }catch (Exception e){
            result_insertPower=0;
        }
        return result_insertPower;
    }


    /**
     * 删除角色信息和权限
     * @param _role
     * @return
     */
    @Transactional
    public int delRoleAll(Role _role) {
        logger.info("func:delRoleAll %n _role:" + _role);
        roleMapper.delRoleList(_role);
        Integer result_delRolePower = roleMapper.delRolePower(_role);
        return result_delRolePower;
    }

    /**
     * 修改角色信息和权限
     * 先删后加
     * @param _role
     * @return
     */
    @Transactional
    public int updateRoleAll(Role _role) {
        logger.info("func:delRoleAll %n _role:" + _role);
        roleMapper.delRoleList(_role);
        roleMapper.delRolePower(_role);
        roleMapper.insertRole(_role);
        List<RoleMenuRelation> _relation = _role.getPowerData();
        Integer result_insertPower=1;
        if(_relation == null || _relation.size() == 0){
            //没有设置权限则不保存
        }else{
            for(RoleMenuRelation value:_relation){
                //添加 id && roleId
                value.setRoleId(_role.getId());
                value.setId(java.util.UUID.randomUUID().toString());
            }
            result_insertPower = roleMapper.insertPower(_relation);
        }


        return result_insertPower;
    }

    //查重复CODE
    public List<Role> validateLevelCode(Role _role) {
        logger.info("func:validateLevelCode %n _role:" + _role);
        List<Role> dList = roleMapper.validateLevelCode(_role);
        return dList;
    }

    //Menu _menu
    //新角色权限   addrole
    public List<Menu> getPowerModel(String roleId,String  parentId) {
        List<Menu> dList = roleMapper.getPowerModelByRoleId(roleId,parentId);
        return dList;
    }



}
